package com.simple.gateway.core.datasource.unpooled;

import com.simple.gateway.core.datasource.Connection;
import com.simple.gateway.core.datasource.DataSource;
import com.simple.gateway.core.datasource.DataSourceType;
import com.simple.gateway.core.datasource.connection.db.MysqlConnection;
import com.simple.gateway.core.datasource.connection.http.DefaultHttpConnection;
import com.simple.gateway.core.datasource.connection.rpc.DubboConnection;
import com.simple.gateway.core.mapping.HttpStatement;
import com.simple.gateway.core.session.Configuration;

/**
 * 项目: simple-gateway-core
 *
 * 功能描述: 无池化数据源
 *
 * @author: WuChengXing
 *
 * @create: 2023-08-04 16:58
 **/
public class UnpooledDataSource implements DataSource {

    private Configuration configuration;
    private HttpStatement httpStatement;
    private DataSourceType dataSourceType;

    @Override
    public Connection getConnection() {
        switch (dataSourceType) {
            case HTTP:
                return new DefaultHttpConnection("");
            case RPC_DUBBO:
                return new DubboConnection(configuration, httpStatement);
            case DB_MYSQL:
                return new MysqlConnection(configuration, httpStatement);
            default:
                break;
        }
        throw new RuntimeException("DataSourceType：" + dataSourceType + "没有对应的数据源实现");
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public void setHttpStatement(HttpStatement httpStatement) {
        this.httpStatement = httpStatement;
    }

    public void setDataSourceType(DataSourceType dataSourceType) {
        this.dataSourceType = dataSourceType;
    }
}
